import { BASE_URL, token_alia } from '@/global.config'
import axios from 'axios'
import { useMessage } from './utils'

export default function http (url: string, payload: object) {
  return new Promise((resolve, reject) => {
    axios({
      url: `${BASE_URL}/v1${url}`,
      method: 'post',
      data: {
        ...payload
      },
      headers: {
        Authorization: 'Bearer ' + localStorage.getItem(token_alia)
      }
    }).then(res => {
      resolve(res.data)
    }).catch((error: any) => {
      console.log('报错啦', error)
      if ([401].includes(error.response.status)) {
        localStorage.removeItem(token_alia)
        window.location.reload()
      } else {
        useMessage(error)
      }
    })
  })
}